﻿using Cdx.Bl;
using Cdx.Bl.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;

namespace Cdx.Ui.Models
{
    public class Model
    {
        #region Private fields

        private const string KEY_MODEL = "MODEL_KEY";
       
        private string m_ConnectionString;
        #endregion

        #region Ctor
        private Model()
        {
            InitializeAppSettingsKeys();

            Engine = new Engine(m_ConnectionString, InputDirectory);
            Status = new Status();
        }
        #endregion

        #region Private methods

        private void InitializeAppSettingsKeys()
        {
            bool isDebug = bool.Parse(ConfigurationManager.AppSettings["IsDebugMode"]);

            if (isDebug == true)
            {
                m_ConnectionString = ConfigurationManager.AppSettings["ConnectionStringDebug"];
            }
            else
            {
                m_ConnectionString = ConfigurationManager.AppSettings["ConnectionStringRelease"];
            }
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Single tone implementation
        /// </summary>
        public static Model Instance
        {
            get
            {
                if (HttpContext.Current.Session[KEY_MODEL] == null)
                {
                    HttpContext.Current.Session[KEY_MODEL] = new Model();
                }

                return (Model)HttpContext.Current.Session[KEY_MODEL];
            }
        }

        public Engine Engine { get; private set; }

        public Status Status { get; private set; }
        
        public E_User CurrentUser { get; set; }

        public static string InputDirectory
        {
            get 
            {
                return ConfigurationManager.AppSettings["ImagesFolder"];
            }
        }
        #endregion
    }
}